package com.ruoyi.web.controller.user;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TbFactory;
import com.ruoyi.system.service.ITbFactoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 供应厂商列Controller
 * 
 * @author ruoyi
 * @date 2020-06-24
 */
@Controller
@RequestMapping("/user/factory")
public class TbFactoryController extends BaseController
{
    private String prefix = "user/factory";

    @Autowired
    private ITbFactoryService tbFactoryService;

    @RequiresPermissions("user:factory:view")
    @GetMapping()
    public String factory()
    {
        return prefix + "/factory";
    }

    /**
     * 查询供应厂商列列表
     */
    @RequiresPermissions("user:factory:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbFactory tbFactory)
    {
        startPage();
        List<TbFactory> list = tbFactoryService.selectTbFactoryList(tbFactory);
        return getDataTable(list);
    }

    /**
     * 导出供应厂商列列表
     */
    @RequiresPermissions("user:factory:export")
    @Log(title = "供应厂商列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbFactory tbFactory)
    {
        List<TbFactory> list = tbFactoryService.selectTbFactoryList(tbFactory);
        ExcelUtil<TbFactory> util = new ExcelUtil<TbFactory>(TbFactory.class);
        return util.exportExcel(list, "factory");
    }

    /**
     * 新增供应厂商列
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存供应厂商列
     */
    @RequiresPermissions("user:factory:add")
    @Log(title = "供应厂商列", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TbFactory tbFactory)
    {
        return toAjax(tbFactoryService.insertTbFactory(tbFactory));
    }

    /**
     * 修改供应厂商列
     */
    @GetMapping("/edit/{fid}")
    public String edit(@PathVariable("fid") Long fid, ModelMap mmap)
    {
        TbFactory tbFactory = tbFactoryService.selectTbFactoryById(fid);
        mmap.put("tbFactory", tbFactory);
        return prefix + "/edit";
    }

    /**
     * 修改保存供应厂商列
     */
    @RequiresPermissions("user:factory:edit")
    @Log(title = "供应厂商列", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TbFactory tbFactory)
    {
        return toAjax(tbFactoryService.updateTbFactory(tbFactory));
    }

    /**
     * 删除供应厂商列
     */
    @RequiresPermissions("user:factory:remove")
    @Log(title = "供应厂商列", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tbFactoryService.deleteTbFactoryByIds(ids));
    }
}
